package cn.hzking.gjmall.activitys.my;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.delegates.my.OrderDelegate;
import cn.hzking.gjmall.view.SimpleOptionView;

public class MyOrderActivity extends ColorStatuBarActivity {
    public static final int TOTLEORDER = 0;
    public static final int WAITPAY = 1;
    public static final int WAITSEND = 2;
    public static final int WAITRECIVE = 3;
    public static final int COLSED = 4;
    public static final int BACKMONEY = 5;
    public static final int COMPLETE = 6;
    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.myorder_framelayout)
    FrameLayout myorderFramelayout;
    private OrderDelegate[] orderDelegates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        simpleTileView.setRightOption(R.mipmap.dingdan_caidan, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop(v);

            }
        });
        OrderDelegate totleOrderDelegate = OrderDelegate.newInstance(TOTLEORDER);
        OrderDelegate waitPayDelegate = OrderDelegate.newInstance(WAITPAY);
        OrderDelegate waitSendDelegate = OrderDelegate.newInstance(WAITSEND);
        OrderDelegate waitReciveDelegate = OrderDelegate.newInstance(WAITRECIVE);
        OrderDelegate colsedOrderDelegate = OrderDelegate.newInstance(COLSED);
        OrderDelegate backMoneyDelegate = OrderDelegate.newInstance(BACKMONEY);
        OrderDelegate completeOrderDelegate = OrderDelegate.newInstance(COMPLETE);
        orderDelegates = new OrderDelegate[]{totleOrderDelegate, waitPayDelegate, waitSendDelegate, waitReciveDelegate, colsedOrderDelegate, backMoneyDelegate, completeOrderDelegate};
        Intent intent = getIntent();
        int fragmentId = intent.getIntExtra("fragmentId", -1);
        switchFragment(fragmentId);
    }

    private void showPop(View v) {
        View view = View.inflate(this, R.layout.layout_order_pop, null);
        TextView allTv=view.findViewById(R.id.tv_all);
        TextView waitpayTv=view.findViewById(R.id.tv_waitpay);
        TextView waitsendTv=view.findViewById(R.id.tv_waitsend);
        TextView waitreceiveTv=view.findViewById(R.id.tv_waitreceive);
        TextView closeTv=view.findViewById(R.id.tv_close);
        TextView refundTv=view.findViewById(R.id.tv_refund);
        TextView finishTv=view.findViewById(R.id.tv_finish);
        final PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //测量view 注意这里，如果没有测量  ，下面的popupHeight高度为-2  ,因为LinearLayout.LayoutParams.WRAP_CONTENT这句自适应造成的
     //   view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = view.getMeasuredWidth();    //  获取测量后的宽度
        int popupHeight = view.getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        // 允许点击外部消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //注意这里如果不设置，下面的setOutsideTouchable(true);允许点击外部消失会失效
        popupWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        popupWindow.setFocusable(true);
        // 获得位置 这里的v是目标控件，就是你要放在这个v的上面还是下面
        v.getLocationOnScreen(location);
        // popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);  //设置动画
        //这里就可自定义在上方和下方了 ，这种方式是为了确定在某个位置，某个控件的左边，右边，上边，下边都可以
        popupWindow.showAsDropDown(v,-40,-20, Gravity.LEFT|Gravity.BOTTOM);
        allTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(TOTLEORDER);
                popupWindow.dismiss();
            }
        });
        waitpayTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(WAITPAY);
                popupWindow.dismiss();
            }
        });
        waitsendTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(WAITSEND);
                popupWindow.dismiss();
            }
        });
        waitreceiveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(WAITRECIVE);
                popupWindow.dismiss();
            }
        });
        closeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(COLSED);
                popupWindow.dismiss();
            }
        });
        refundTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(BACKMONEY);
                popupWindow.dismiss();
            }
        });
        finishTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFragment(COMPLETE);
                popupWindow.dismiss();
            }
        });
    }

    private void switchFragment(int fragmentId) {
        if (fragmentId == TOTLEORDER) {//全部订单
            loadMultipleRootFragment(R.id.myorder_framelayout, 0, orderDelegates);
            simpleTileView.setTitle("全部订单");
        } else if (fragmentId == WAITPAY) {//待付款订单
            loadMultipleRootFragment(R.id.myorder_framelayout, 1, orderDelegates);
            simpleTileView.setTitle("待付款");
        } else if (fragmentId == WAITSEND) {//待发货订单
            loadMultipleRootFragment(R.id.myorder_framelayout, 2, orderDelegates);
            simpleTileView.setTitle("待发货");
        } else if (fragmentId == WAITRECIVE) {//待收货
            loadMultipleRootFragment(R.id.myorder_framelayout, 3, orderDelegates);
            simpleTileView.setTitle("待收货");
        } else if (fragmentId == COLSED) {//交易关闭
            loadMultipleRootFragment(R.id.myorder_framelayout, 4, orderDelegates);
            simpleTileView.setTitle("交易关闭");
        } else if (fragmentId == BACKMONEY) {//退款售后
            loadMultipleRootFragment(R.id.myorder_framelayout, 5, orderDelegates);
            simpleTileView.setTitle("退款售后");
        } else if (fragmentId == COMPLETE) {//完成订单
            loadMultipleRootFragment(R.id.myorder_framelayout, 6, orderDelegates);
            simpleTileView.setTitle("完成订单");
        }
    }
    private void showFragment(int fragmentId) {
        showHideFragment(orderDelegates[fragmentId]);
        if (fragmentId == TOTLEORDER) {//全部订单
            simpleTileView.setTitle("全部订单");
        } else if (fragmentId == WAITPAY) {//待付款订单
            simpleTileView.setTitle("待付款");
        } else if (fragmentId == WAITSEND) {//待发货订单
            simpleTileView.setTitle("待发货");
        } else if (fragmentId == WAITRECIVE) {//待收货
            simpleTileView.setTitle("待收货");
        } else if (fragmentId == COLSED) {//交易关闭
            simpleTileView.setTitle("交易关闭");
        } else if (fragmentId == BACKMONEY) {//退款售后
            simpleTileView.setTitle("退款售后");
        } else if (fragmentId == COMPLETE) {//完成订单
            simpleTileView.setTitle("完成订单");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this );
    }
}
